package com.zueb.web.servlet;

import com.zueb.pojo.Classes;
import com.zueb.service.FacultyService;
import com.zueb.service.impl.FacultyServiceImpl;
import com.zueb.util.MD5;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    //静态消息文本
    static String message = "";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("login")){
            login(request, response);  //登录跳转
        } else if (action.equals("register")){
            register(request, response); //用户注册
        }
    }

    /**
     * 用户注册页面
     * @param request
     * @param response
     */
    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取学院列表
        FacultyService facultyService = new FacultyServiceImpl();
//        List<Faculty> faculties = facultyService.getAllFaculty();
        List<Classes> classes = facultyService.getClassesByFaculty(1);


        //将列表放入页面
        request.setAttribute("classes",classes);
        //跳转页面
        request.getRequestDispatcher("register.jsp").forward(request, response);
    }

    /**
     * 登录跳转
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收页面身份参数
        String role = request.getParameter("role");
        //接收页面传来的数据（用户名、密码、验证码）
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");
        String checkCode = request.getParameter("checkCode");
        //获取Session
        HttpSession session = request.getSession();
        //获取服务器保存的验证码
        String realCheckCode = session.getAttribute("checkCode").toString();

        if (checkCode.equals(realCheckCode)){
            //封装数据
            Map<String, String> user = new HashMap<>();
            user.put("userName", userName);
            user.put("passWord", MD5.md5(passWord));
            //判断用户类型（管理员/教职工或学生）
            if (role.equals("admin")){
                //管理员账户
                message = "<script>window.open('AdminServlet?action=adminLogin','_self');</script>";
            } else if (role.equals("teacher") || role.equals("student")){
                //教职工或学生账户
                message = "<script>window.open('UserServlet?action=userLogin','_self');</script>";
            }
            //把登录用户放入页面
            session.setAttribute("user", user);
        } else {
            request.setAttribute("userName",userName);
            message = "<script>alert('验证码错误！');window.open('login.jsp','_self')</script>";
        }
        //把信息放入页面
        request.setAttribute("message", message);
        //跳转页面
        request.getRequestDispatcher("message.jsp").forward(request, response);
    }

}
